package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;


import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.Department;
import ywpt.model.PositionInfo;
import ywpt.web.common.PublicFunction;

public class PositionServiceImpl extends HibernateEntityDao<PositionInfo> implements PositionService{

	public PositionServiceImpl(){
		
	}
	@Override
	public PositionInfo getPositionInfoById(Long id) {
		return this.get(id);
	}
	@Override
	public String createPositionCode() {
		DetachedCriteria query = DetachedCriteria.forClass(PositionInfo.class, "position");
		String positionCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<PositionInfo> positionInfos = this.getPositionInfos(query);
		if ( positionInfos.iterator().hasNext() )	
		{
			code = (positionInfos.iterator().next().getPositionCode()).substring(2);
			maxId = new Integer(code);
		}
		positionCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return ("ZW"+positionCode);
	}
	@Override
	public int getPositionInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<PositionInfo> getPositionInfos(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}
	@Override
	public List<PositionInfo> getPositionInfos() {
		return this.getAll();
	}
	@Override
	public void removePositionInfo(Long id) {
		this.removeById(id);
	}
	@Override
	public void savePositionInfo(PositionInfo positionInfo, Long departId) {
		if(departId != null){
			positionInfo.setDepart(this.get(Department.class, departId));
		}
		super.save(positionInfo);
	}
	@Override
	public void updatePositionInfo(PositionInfo positionInfo, Long departId) {
		if(departId != null){
			positionInfo.setDepart(this.get(Department.class, departId));
		}
		super.update(positionInfo);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<PositionInfo> getPositionInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<PositionInfo> getPositionsByPositionName(String positionName,Long departId, int start,
			int size) {
		DetachedCriteria query = DetachedCriteria.forClass(PositionInfo.class, "client");
		if ( positionName != null && !positionName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("positionName", "%" + positionName + "%"),  
							          Restrictions.like("positionCode", "%" + positionName + "%")));
		}
		List<PositionInfo> positionList = new ArrayList<PositionInfo>();
		List<PositionInfo> positionLists = new ArrayList<PositionInfo>();
		PositionInfo position =new PositionInfo();
		if(departId != null){
			query.add(Restrictions.eq("depart", this.get(Department.class, departId)));
		}
		query.addOrder(Order.asc("positionCode"));				
		positionList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<PositionInfo> itr = positionList.iterator(); itr.hasNext(); i++ ) 
		{
			position = itr.next();
			String ss = PublicFunction.format(position.getPositionCode() + "", 14, "  ", 1)
			          + position.getPositionName(); 
			
			position.setPositionName(ss);
			positionLists.add(position);
		}
		return positionLists;
	}

	@Override
	public int getPositionsCountByName(String positionName,Long departId) {
		DetachedCriteria query = DetachedCriteria.forClass(PositionInfo.class, "position");
		if ( positionName != null && !positionName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("positionName", "%" + positionName + "%" ),  
							          Restrictions.like("positionCode", "%" + positionName + "%" )));
		}
		if(departId != null){
			query.add(Restrictions.eq("depart", this.get(Department.class, departId)));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getDepartments() {
		String hql="select distinct(depart) from PositionInfo";
		return this.find(hql);
	}	
	
}
